import { _decorator, Component, Node } from 'cc';
import { ColdTimer } from './ColdTimer';
const { ccclass, property } = _decorator;
/**
 * 技能按钮
 */
@ccclass('SkillButton')
export class SkillButton extends Component {

    @property(Node)
    coldTimer: Node;

    private _isColding: boolean = false;
    private _onclick: () => void;
    private _target: any;

    coldTime: number = 3;

    public get isColding(): boolean {
        return this._isColding;
    }

    public set isColding(value: boolean) {
        this._isColding = value;
        this.coldTimer.active = value;
    }


    protected onEnable(): void {
        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);

        this.coldTimer.getComponent(ColdTimer).onColdDown(() => this.isColding = false);
        this.isColding = false;
    }
    protected onDisable(): void {
        this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
    }

    start() {

    }

    update(deltaTime: number) {

    }
    private onTouchStart() {
        if (this.isColding) {
            return;
        }
        
    }
    private onTouchEnd() {
        if (this.isColding) {
            return;
        }
        this.isColding = true;
        this.coldTimer.getComponent(ColdTimer).startColdTime(this.coldTime);
        this._onclick?.call(this._target);
    }
    private onTouchCancel() {
        if (this.isColding) {
            return;
        }
    }
    onClick(onClick: () => void, target?: any) {
        this._onclick = onClick;
        this._target = target;

    }
}


